package com.huixiaodian.baselibrary.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.huixiaodian.baselibrary.R;


public class HeaderBar extends ConstraintLayout {

    private String titleText;
    private String optionText;

    private int titleTextColor;
    private int optionTextColor;

    private Drawable ivBackDrawable;
    private Drawable backgroundDrawable;

    private TextView tvTitle;
    private TextView tvOption;
    private ImageView ivBack;
    private ConstraintLayout constraintLayout2;

    public HeaderBar(Context context) {
        this(context, null);
    }

    public HeaderBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public HeaderBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.HeaderBar);
        titleText = typedArray.getString(R.styleable.HeaderBar_HeaderBar_titleText);
        optionText = typedArray.getString(R.styleable.HeaderBar_HeaderBar_optionText);
        titleTextColor = typedArray.getColor(R.styleable.HeaderBar_HeaderBar_titleTextColor,
                Color.parseColor("#333333"));
        optionTextColor = typedArray.getColor(R.styleable.HeaderBar_HeaderBar_optionTextColor,
                Color.parseColor("#333333"));
        ivBackDrawable = typedArray.getDrawable(R.styleable.HeaderBar_HeaderBar_ivBackDrawable);
        backgroundDrawable = typedArray.getDrawable(R.styleable.HeaderBar_HeaderBar_backgroundDrawable);
        initHeaderBarView(context);
        typedArray.recycle();
    }

    private void initHeaderBarView(Context context){
        View constraintLayout = View.inflate(context, R.layout.layout_header_bar, this);
        constraintLayout2 = constraintLayout.findViewById(R.id.layout_parent);
        ivBack = constraintLayout.findViewById(R.id.iv_back);
        tvTitle = constraintLayout.findViewById(R.id.tv_title);
        tvOption = constraintLayout.findViewById(R.id.tv_option);
        if(ivBackDrawable != null) ivBack.setImageDrawable(ivBackDrawable);
        if(backgroundDrawable != null) constraintLayout.setBackground(backgroundDrawable);
        tvTitle.setText(titleText);
        tvOption.setText(optionText);
        tvTitle.setTextColor(titleTextColor);
        tvOption.setTextColor(optionTextColor);
        ivBack.setOnClickListener(v->{
            if(context instanceof Activity) ((Activity) context).finish();
        });
    }

    public TextView getOptionTextView(){
        return tvOption;
    }

    public void setTitle(String textTitle){
        if(tvTitle != null) tvTitle.setText(textTitle);
    }

    public void setIvBack(Drawable drawable){
        ivBack.setImageDrawable(drawable);
    }

    public void setTitleTextColor(int titleTextColor) {
        tvTitle.setTextColor(titleTextColor);
    }

    @Override
    public void setBackgroundColor(int color) {
        super.setBackgroundColor(color);
    }

    public ImageView getIvBack(){
        return ivBack;
    }
}
